package com.util;

import com.annotation.After;
import com.annotation.Before;
import com.annotation.Run;
import com.dao.GoodDao;
import com.entity.Good;
import com.entity.Message;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

/**
 * good server
 *方法组装，增强健壮性，方便调用
 * 实现自定义的方法
 */
public class GoodUtil {

    private static InputStream in;
    private static SqlSession session;
    private static GoodDao goodDao;


    //初始化Session
    private static void init(){

        try {
            //1.读取配置文件，生成字节输入流
            in = Resources.getResourceAsStream("SqlMapConfig.xml");
            InputStreamReader reader = new InputStreamReader(in);

            System.out.println(reader);
            //2.获取SqlSessionFactory
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
            //3.获取SqlSession对象
            session = factory.openSession(true);
            //4.获取dao的代理对象
            goodDao = session.getMapper(GoodDao.class);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //销毁相关流
    private static void destroy(){

        try {
            //提交事务
            session.commit();
            //6.释放资源
            if (session != null) {
                session.close();
            }
            if (in != null) {
                in.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 查询所有食物，空表时返回null
     * @return
     * @throws Exception
     */
    public static Message findFood()  {
        init();
        Message msg = MessUtil.packMess(goodDao.findFood());
        destroy();
        return msg;
    }

    /**
     * 查询所有日用品，空表时返回null
     * @return
     * @throws Exception
     */
    public static Message findNess() {
        init();
        Message msg = MessUtil.packMess(goodDao.findNess());
        destroy();
        return msg;
    }

    /**
     * 查询所有化妆品，空表时返回null
     * @return
     * @throws Exception
     */
    public static Message findCos()  {
        init();
        Message msg = MessUtil.packMess(goodDao.findCos());
        destroy();
        return msg;
    }

    /**
     * 查询所有饮料，空表时返回null
     * @return
     * @throws Exception
     */
    public static Message findDrink() {
        init();
        Message msg = MessUtil.packMess(goodDao.findDrink());
        destroy();
        return msg;
    }

    /**
     * 包装方法
     * 讲GoodList包装成Good二维数组并返回
     */
    public static Message packString(List<Good> list){


        String[][] strs = new String[list.size()][6];
        int i = 0;

        for(Good good : list) {

            String[] str = {good.getName(),String.valueOf(good.getCharge()),String.valueOf(good.getAmount()),good.getFactory(),good.getMark(),good.getOth()};
            strs[i] = str;

            i++;
        }

        if(i == 0) {
            Message message = new Message();
            message.setStrings(null);
            return message;
        }else {
            return MessUtil.packMess(strs);
        }



    }

    /**
     * 添加商品
     * 若已存在，提示添加失败
     */
    public static Message addGood(Good good)  {

        init();
        List<Good> g = goodDao.findByName(good.getName());
        if(g.size()==0){
            goodDao.addGood(good);
            destroy();
            return MessUtil.packMess(true,"添加成功");
        }else{
            destroy();
            return MessUtil.packMess(false,"产品已存在,添加失败");
        }

    }

    /**
     * 根据商品名称删除商品
     */
    public static Message deleteGood(String name) {

        init();
        goodDao.deleteGood(name);
        destroy();
        return MessUtil.packMess(true,"删除成功");

    }

    /**
     * 根据商品名称修改商品信息
     */
    public static Message updateGood(Good good) {


        init();
        goodDao.updateGood(good);
        destroy();
        return MessUtil.packMess(true, "修改成功");

    }

    public static Message findByName(String name)  {


        init();
        List<Good> goods = goodDao.findByName(name);
        destroy();
        return MessUtil.packMess(goods);


    }

    /**
     * 根据名称销售产品
     */
    public static Message sellGood(String name,int amount){
        init();
        goodDao.sellGood(name,amount);
        destroy();
        return MessUtil.packMess(true,"销售成功");
    }
}
